import { constantRoutes, asyncRoutes } from "@/router";

const state = () => {
  return {
    // 路由表数组   初始值是静态路由表的值
    routes: constantRoutes,
  };
};

const mutations = {
  // 设置路由表
  setRoutes: (state, showAsyncRoutes) => {
    // state.routes.push(...showAsyncRoutes);
    state.routes = [...constantRoutes, ...showAsyncRoutes];
  },
  //删除路由表
  removeRouters: (state) => {
    state.routes = [];
  },
};

const actions = {
  // 筛选动态路由表
  filterAsyncRouteAction: ({ commit }, menus) => {
    //基于动态路由表和menus数组筛选
    const showAsyncRoutes = asyncRoutes.filter((item) =>
      menus.includes(item.children[0].name)
    );
    //提交mutation
    commit("setRoutes", showAsyncRoutes);
    // 返回筛选结果
    return showAsyncRoutes;
  },
};
export default {
  //开启命名空间    防止崇明
  namespaced: true,
  state,
  mutations,
  actions,
};
